<script type="text/javascript">

var paginationTotal = <{$total}>;//记录总数
$(function(){
	initData(0); //初始化分页<{$lang.data}>
	clear();
	$("#add").dialog({
		autoOpen:false,
		width:400,
		height:440,
		modal:true,
		show:"slide",
		buttons: {
			'<{$lang.ok}>': function() {
				save();
			},
	    	'<{$lang.cancel}>': function() {
				$(this).dialog('close');
			 },
		}
	});
	
	$("#right_confirm").dialog({
		autoOpen:false,
		modal:true,
		height:160,
		show:"slide",
		buttons: {
			'<{$lang.ok}>': function() {
				loading();
				$.ajax({
					type: "post",
					async: false,
					dataType: "json",
					url: "/warehouse/products/delete", 
					data: "id=" + Uid,
					success: function(json) { 
						if(json.ask=='1'){
							initData(0);
							messags="<{$lang.success}><{$lang.delete}>！";
						}else{
							messags="<{$lang.delete}><{$lang.fail}>！";
						}
						$("#message").html(messags);
					}
				});
				closeLoading();
				$(this).dialog('close');
			},
	    	'<{$lang.cancel}>': function() {
					$(this).dialog('close');
			 },
		}

	});
	
});

function loadData(page,pageSize){
	var customer_name = $('[name=customer_name]').val();
	var warehouse_id_sch = $('[name=warehouse_id_sch]').val();
	var wa_code_sch = $('[name=wa_code_sch]').val();
	var wh_id_sch = $('[name=wh_id_sch]').val();
	
	loading();
	$.ajax({ //这里使用到Jquery的ajax<{$lang.method}>
		type: "POST",
		async: false,
		dataType: "json",
		url: "/warehouse/products/list", //请求的处理url
		data:{
			'page':page,'pageSize':pageSize,
			'customer_name':customer_name,
			'warehouse_id_sch':warehouse_id_sch,
			'wa_code_sch':wa_code_sch,'wh_id_sch':wh_id_sch,			
		},
		success: function(json) {
			var list = '';
			if(json.ask != 1){
				list = "<td colspan='7' class=''>&nbsp;<{$lang.no_data}></td>";
			}else {
				paginationTotal=json.total;							
				$.each(json.result, function(key, val){				
					list += "<tr>";					
					list += "<td>"+val.wp_id+"</td>";					
					list += "<td>"+val.wa_code+"</td>";
					list += "<td>"+val.wh_code+"</td>";
					list += "<td>"+val.ws_code+"</td>";
					list += "<td>"+val.customer_name+"</td>";
					list += "<td>"+val.product_sku+"</td>";
					list += "<td>"+val.wp_quantity+"</td>";					
					list += "<td>"+val.product_title+"</td>";
					
					list += "<td><a href=\"javascript:editProducts("+val.wp_id+");\"><img alt='<{$lang.edit}>' src='/images/icon/edit.gif'></a>";				
					list += "| "+ "<a href=\"javascript:delelteInfo("+val.wp_id+")\"><img alt='<{$lang.delete}>' src='/images/icon/delete.gif'></a></td>";
					
					list += "</tr>";
				});
				$("#list").html(list);
				closeLoading();
			}
		}
	}); 
}

function search(){	
	initData(0);
}

function editProducts(id){
	clear();
	if(id){
		$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "/warehouse/products/single", //请求的处理url
			data:{'id':id,},
			success: function(json) {
				if(json.ask == 1 ) {
					var val = json.result;
					$('[name=wp_id]').val(val.wp_id);
					$('[name=warehouse_id]').val(val.warehouse_id);
					$('[name=ws_code]').val(val.ws_codeh);
					$('[name=product_id]').val(val.product_id);
					
					$('[name=wp_quantity]').val(val.wp_quantity);
					$('[name=wh_create_date]').val(val.wh_create_date);
					
				}
				$("#add").dialog("open");
			}
		});
	}else{
		$("#add").dialog("open");
	}
}

function save(){
	vForm='';
	vForm = $("#createForm").serializeArray();
	
	var tip = '';
	$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "/warehouse/products/edit", //请求的处理url
			data:vForm,
			success: function(json) {
				if(json.ask){
					tip = '<{$lang.operation}><{$lang.success}>';					
				}else{
					tip = '<{$lang.operation}><{$lang.fail}>';	
				}
				loadData(paginationCurrentPage,paginationPageSize);
				$("#add").dialog("close");
				$('#message').text(tip);
			}
	});
}

function closebox(){
	$("#add").dialog("close");
}

//<{$lang.delete}>
function delelteInfo(id){
	if(id==''||id==undefined){
		return false;
	}
	Uid = id;
	$('#right_confirm').dialog('open');
}


function clear(){
	$('[name=package_name]').val('');		//必须<{$lang.clear}>，否则会报错
	$('[name=wp_id]').val('');
	$('[name=warehouse_id]').val('');
	$('[name=ws_code]').val('');
	$('[name=product_id]').val('');
					
	$('[name=wp_quantity]').val('');
	$('[name=wh_create_date]').val('');	
}

//获取<{$lang.area}>下拉<{$lang.list}>
function selectArea(val){
	if(val){
		var options = '';
		options += '<option value=""><{$lang.pls}></option>';
		$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "/warehouse/products/getarea", //请求的处理url
			data:{'id':val,},
			success: function(json) {
				if(json.ask){					
					$.each(json.result, function(key, val){						
						options += '<option value="'+val.wa_code+'">'+val.wa_name+'</option>';	
					});
					
				}
			}
		});
		$('#wa_code_sch').empty().append(options);
	}
}

//获取<{$lang.room}>下拉<{$lang.list}>
function selectHouse(val){
	if(val){
		var options = '';
		options += '<option value=""><{$lang.pls}></option>';
		$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "/warehouse/products/gethouse", //请求的处理url
			data:{'id':val,},
			success: function(json) {				
				if(json.ask){					
					$.each(json.result, function(key, val){
						options += '<option value="'+val.wh_code+'">'+val.wh_code+'</option>';	
					});						
				}
			}
		});
		$('#wh_code_sch').empty().append(options);
	}
}

</script>
<style type="text/css">
.button{background:url(images/left-menu-li.gif) repeat-x left top #F1F6F9; border:1px solid #318DAC; font-weight:bold; padding:2px 5px; font-weight:lighter; color:#318DAC; vertical-align:middle; -moz-border-radius:3px; -webkit-border-radius:3px; margin:auto 5px}
</style>

<div id="right_confirm" style="display: none;">
	<p style="text-align:center;font-size: 14px;color: red;font-weight: bold;">
	<{$lang.delete_sure}>？
	</p>
</div>

<div id="iTitle">
	<div id="iTitleSub">
	
		<form id="sForm" name="sForm">
		<div>
		<label>
			<{$lang.customer}><{$lang.name}>:<input id="customer_name" class="myInput" type="text" name="customer_name">
		</label>&nbsp;
		<label>
			<{$lang.warehouse}>:<select name="warehouse_id_sch" id="warehouse_id_sch" onchange="selectArea(this.value)">
				<option value=""><{$lang.pls}></option>
				<{foreach key=key item=item from=$warehouse}>
				<option value="<{$item.warehouse_id}>"><{$item.warehouse_name}></option>
				<{/foreach}>
			</select>
		</label>&nbsp;
		<label>
			<{$lang.area}>:<select name="wa_code_sch" id="wa_code_sch" onchange="selectHouse(this.value)">
				<option value=""><{$lang.pls}></option>
			</select>
		</label>&nbsp;
		<label>
			<{$lang.room}>:<select name="wh_code_sch" id="wh_code_sch">
				<option value=""><{$lang.pls}></option>
			</select>
		</label>&nbsp;
		
		<input class="button" type="button" value="<{$lang.search}>" onclick="search();">&nbsp;&nbsp;
		<input class="button" type="button" value="<{$lang.add}>"  id="addButton" onclick="editProducts('')">&nbsp;&nbsp;
		</div>
		</form>
		
	</div>
	<div id="message" style="color:red;"></div>
	<div class="myClear"></div>
</div><!-- id="iTitle" 结束 -->

<h3><{$lang.packaging_material}><{$lang.list}></h3>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">	
	<thead><tr>
    	<th>ID</th>
    	<th><{$lang.area}></th>
    	<th><{$lang.room}></th>
   		<th><{$lang.ws_code}></th>
    	<th><{$lang.customer}><{$lang.name}></th>
        <th><{$lang.sku}></th>
        
        <th><{$lang.quantity}></th>
        <th><{$lang.product}><{$lang.name}></th>       
        <th><{$lang.operation}></th>
	</tr></thead>
    
	<tbody id="list"></tbody>
	
</table>

<div class="pagination"></div>

<style type="text/css">
div#add{
	text-align:center;
}
div.info{
	text-align:left;
}
div.item{
	margin-bottom: 12px;
}
input.text {
    height: 22px;    
    margin-left: 8px;
}
span.text{
	margin-left: 8px;
}
span.userInfoLeft{
	display:block;
	float:left;
	width:100px;
}
</style>

<div id="add" title="<{$lang.warehouse}><{$lang.product}><{$lang.msg}><{$lang.edit}>" style="display:none;">
 
	<div class="info">
		<div class="validateTips" id="tips"></div>
		
		<form id="createForm" name="createForm" >
				
		<div class="item">
			<span class="userInfoLeft"><{$lang.warehouse}><{$lang.name}>:</span>
			<span class="text userInfoRight">
				<input type="hidden" name="wp_id" id="wp_id" />
				<select name="warehouse_id" id="warehouse_id" class="ui-widget-content ui-corner-all" >
					<option value='1'>深圳</option>
					<option value='2'>English</option>
				</select>		
			</span>
			<div style="clear: both;"></div>
		</div>
		
		<div class="item">
			<span class="userInfoLeft">ws_code:</span>
			<span class="userInfoRight">
				<input type="text" name="ws_code" id="ws_code" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>		
		<div class="item">
			<span class="userInfoLeft"><{$lang.product}>:</span>
			<span class="userInfoRight">
				<input type="text" name="product_id" id="product_id" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>
		<div class="item">
			<span class="userInfoLeft"><{$lang.quantity}>:</span>
			<span class="userInfoRight">
				<input type="text" name="wp_quantity" id="wp_quantity" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>
		<div class="item">
			<span class="userInfoLeft"><{$lang.master_slave}><{$lang.ws_code}>:</span>
			<span class="userInfoRight">
				<select name="wh_primary" id="wh_primary" class="ui-widget-content ui-corner-all" >
					<option value='1'>master</option>
					<option value='2'>slave</option>
				</select>				
			</span>
			<div style="clear: both;"></div>
		</div>
		
		</form>
	
	</div>
</div>